//
//  DataBaseHandler.m
//  project_delicious
//
//  Created by dlios on 15-3-31.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DataBaseHandler.h"

@implementation DataBaseHandler
+(DataBaseHandler *)shareInstance
{
    static DataBaseHandler *db = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        db = [[DataBaseHandler alloc] init];
        [db openDB];
        [db createTable];
    });
    return db;
}
- (void)judgeResult:(int)result type:(NSString *)type
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", type);
    } else {
        NSLog(@"%@失败, 失败编号 %d", type, result);
    }
}

- (void)openDB
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [path stringByAppendingString:@"/guo.db"];
    NSLog(@"%@", dbPath);
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    [self judgeResult:result type:@"打开数据库"];
}
- (void)closeDB
{
    int result = sqlite3_close(dbPoint);
    [self judgeResult:result type:@"关闭数据库"];
}
- (void)createTable
{
    NSString *sqlStr = @"create table food (foodStr text primary key, foodStr1 text, cookStr text, nameLabel text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"创建表"];
    
}
- (void)insertFood:(Food *)food
{
    NSString *insertFood = [NSString stringWithFormat:@"insert into food (foodStr, foodStr1, cookStr, nameLabel) values ('%@', '%@', '%@', '%@')", food.foodStr, food.foodStr1, food.cookStr, food.nameLabel];
    NSLog(@"%@", insertFood);
    
    int result = sqlite3_exec(dbPoint, [insertFood UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"添加美食"];
    
    
}
- (void)deleteFood:(Food *)food
{
    NSLog(@"%@", food.foodStr);
    NSString *deleteFood = [NSString stringWithFormat:@"delete from food where foodStr = '%@' ", food.foodStr];
    NSLog(@"%@", deleteFood);
    int result = sqlite3_exec(dbPoint, [deleteFood UTF8String], NULL, NULL, NULL);
    
    [self judgeResult:result type:@"删除美食"];
}

- (NSMutableArray *)selectAll
{
    NSString *selectFood = @"select * from food";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, [selectFood UTF8String], - 1, &stmt, NULL);
    NSMutableArray *foodArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            Food *food = [[Food alloc] init];
            const unsigned char *foodStrChar = sqlite3_column_text(stmt, 0);
            NSString *foodStr = [NSString stringWithUTF8String:(const char *) foodStrChar];
            food.foodStr = foodStr;
            
            const unsigned char *foodStrChar1 = sqlite3_column_text(stmt, 1);
            NSString *foodStr1 = [NSString stringWithUTF8String:(const char *) foodStrChar1];
            food.foodStr1 = foodStr1;
            
            const unsigned char *cookStrChar = sqlite3_column_text(stmt, 2);
            NSString *cookStr = [NSString stringWithUTF8String:(const char *) cookStrChar];
            food.cookStr = cookStr;
            
            
            const unsigned char *nameChar = sqlite3_column_text(stmt, 3);
            NSString *nameStr = [NSString stringWithUTF8String:(const char *) nameChar];
            food.nameLabel = nameStr;
            [foodArr addObject:food];
            [food release];

        }
    }
    sqlite3_finalize(stmt);
    return foodArr;
  
}


@end
